import axios from 'axios';
import crowdTestData from 'data/crowdTestData';
import { makeAutoObservable } from 'mobx';
/**
 * 数据源store
 * 描述：数据源页面的数据存储
 * 调用：data-source
 * 时间：20220402
 */
class DataSourceStore {
    constructor() {
        makeAutoObservable(this)
    }  
     /**
     * 人群明细表数据（表体）
     * 描述：人群的标签信息
     * 构造：
     */
    crowdData =  []
    /**
     * 人群明细表数据（表头）
     * 描述：人群的标签信息
     * 构造：
     */
    crowdFields =  []
    

    flowData =  []

    flowFields =  []


    /**
     * 营销信息表数据（表体）
     * 描述：营销信息
     * 构造：
     */
     marketingData =  []
     /**
      * 营销信息表数据（表头）
      * 描述：营销信息
      * 构造：
      */
    marketingFields =  []

    getFields(data){
        let fields = []
        Object.keys(data[0]).map((kItem,index)=>{
            
            const field = {
                title: data[0][kItem],
                dataIndex: kItem,
                key: kItem,
                width: 200,
            }
            fields = [...fields,field ] 
        })
        return fields
    }
    
    apiCrowdData = async() => {
       this.crowdData = crowdTestData.crowdDataTestData
       this.crowdFields = crowdTestData.crowdFieldsTestData
       
        axios.get(axios.defaults.baseURL + `/api/crowd/info/table`).then(
            response => {
                this.crowdData = response.data
                this.crowdFields =  this.getFields(this.crowdData)
                this.crowdData = this.crowdData.slice(1,)
            },
            error => {
                console.log('ERROR',error)
                
            }
        )

    }
    apiFlowData = async() => {
        this.flowData = crowdTestData.flowDataTestData
        this.flowFields = crowdTestData.flowFieldsTestData
         axios.get(axios.defaults.baseURL + `/api/crowd/flow/table`).then(
             response => {
                 this.flowData = response.data
                 this.flowFields =  this.getFields(this.flowData)
                 this.flowData = this.flowData.slice(1,)
             },
             error => {
                 console.log('ERROR',error)
                 
             }
         )
 
     }
    
    apiMarketingData = async() => {
        this.marketingData = crowdTestData.marketingDataTestData
        this.marketingFields = crowdTestData.marketingFieldsTestData
         axios.get(axios.defaults.baseURL + `/api/crowd/marketing/table`).then(
             response => {
                 this.marketingData = response.data
                 this.marketingFields =  this.getFields(this.marketingData)
                 this.marketingData = this.marketingData.slice(1,)
             },
             error => {
                 console.log('ERROR',error)
                 
             }
         )
 
     }
    
    
}

const dataSourceStore = new DataSourceStore()
export default dataSourceStore